{extend name="/base/base" /}
{block name="css"}
    <link rel="stylesheet" href="/static/dream/lib/layui-extend/formSelects/formSelects-v4.css">
{/block}
{block name="content"}
    <form class="layui-form layui-form-pane" enctype="multipart/form-data" >
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属栏目</label>
            <div class="layui-input-block">
                <select name="sort_id" xm-select="sort" xm-select-radio xm-select-search="">
                    <option value="">所属栏目</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标签</label>
            <div class="layui-input-block">
                <select name="tags" xm-select="tags" xm-select-create xm-select-search-type="dl" xm-select-max="5" xm-select-search="">
                    <option value="">请选择所要添加的标签 -- 最多选择5个</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">文章内容</label>
            <div class="layui-input-block">
                <div class="content"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="pull-right">
                <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                <button onclick="window.history.back(-1);" class="layui-btn layui-btn-primary">返回</button>
            </div>
        </div>
    </form>
{/block}

{block name="js"}
<script src="/static/dream/lib/layui-extend/laymd/marked.min.js"></script>
<script>
    layui.config({base: '/static/dream/lib/layui-extend/'})
        .extend({'laymd':'laymd/laymd','formSelects':'formSelects/formSelects-v4'})
        .use(['laymd', 'jquery', 'form', 'formSelects', 'layer', 'cookie'], function() {
            var laymd = layui.laymd,
                $ = layui.jquery,
                form = layui.form,
                formSelects = layui.formSelects,
                layer = layui.layer,
                cookie = layui.cookie;

            //监听提交 表单
            form.on('submit(submit)', function(data){
                data.field.content_html = marked(data.field.content);
                var pm = '';
                $.ajax({
                    url : "{:url('addHandle')}",
                    type : "POST",
                    // contentType: "application/json;charset=utf-8", // 上传内容的格式
                    data : data.field,
                    dataType : "json", // 返回的数据格式
                    success : function(data) {
                        if (data.status == 'success') {
                            cookie.set('pageMsg', {type:'msg',params:{content:'添加成功'}});
                            location.href="/admin/article/table";
                        } else {
                            console.log(data);
                            layer.open({
                                'type':1,
                                'content':data
                            });
                        }
                    },
                    error:function(data){
                        layer.msg(data.msg, {icon: 2});
                    }
                });

                return false;
            });

            // formSelects
            // 启用多选 所有
            formSelects.undisabled();
            formSelects.data('tags', 'server', {
                url: '/admin/tag/tableHandle?returnStyle=layui',
            });
            formSelects.maxTips('tags', function(id, vals, val, max){
                layer.msg('当前多选框最多只允许选择'+max+'个');
            });
            formSelects.data('sort', 'server', {
                url: '/admin/articleSort/getGradeTree?returnStyle=layui',
            });

            // makedown
            //实例化编辑器,可以多个实例
            //改用css选择器,去除init冗余
            var md = laymd('.content', {'name':'content'});

            //内容改变事件
            md.on('change', function () {
                //这里借用marked.js解析效率比HyperDown快,用户可自行找解析器
                this.setPreview(marked(this.getText()));
            });
    });
</script>
{/block}